package com.epalmpay.controller.shop;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.epalmpay.bean.ShopDetailBean;
import com.epalmpay.dto.agent.AgentInDTO;
import com.epalmpay.dto.agent.AgentInfoOutDTO;
import com.epalmpay.dto.agent.AgentListOutDTO;
import com.epalmpay.dto.member.BalanceInDTO;
import com.epalmpay.dto.member.MemberOutDTO;
import com.epalmpay.dto.merchantmanage.GroupListOutDTO;
import com.epalmpay.dto.shopuser.ShopCashierInDTO;
import com.epalmpay.dto.shopuser.ShopCashierListOutDTO;
import com.epalmpay.dto.system.*;

import com.epalmpay.entity.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.system.CrcodeInDTO;
import com.epalmpay.dto.system.ShopInDTO;
import com.epalmpay.dto.system.ShopListOutDTO;
import com.epalmpay.dto.system.ShopOutDTO;
import com.epalmpay.dto.system.ShopSelectiveListOutDTO;
import com.epalmpay.dto.system.ShopSelectiveQueryDTO;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.service.shop.IShopService;

@Controller
@RequestMapping(value = "/merchant/shop")
public class ShopController extends BaseController {


    /**
     *
     */
    @Resource
    private IShopService shopService;
    @Resource
    private ISysLogService sysLogService;

    /**
     * 显示门店分页列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public ShopListOutDTO listUser(ShopInDTO shopInDTO, PageDTO pageDTO) {
        ShopListOutDTO shopList = null;
        try {
            shopList = shopService.listShop(shopInDTO, pageDTO);
            shopList.setErrorCode(Rescode.SUCCESS, "处理成功");
            return shopList;
        } catch (Exception e) {
            shopList.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return shopList;
    }

    /**
     * 根据门店查询二维码列表
     *
     * @return
     */
    @RequestMapping(value = "/listShopCrcode")
    @ResponseBody
    public ShopListOutDTO shopCrcodeList(CrcodeInDTO shopCrcodeInDTO) {
        ShopListOutDTO shopListOutDTO = null;
        try {
            shopListOutDTO = shopService.listShopCrcode(shopCrcodeInDTO);
            shopListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return shopListOutDTO;
        } catch (Exception e) {
            shopListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }

        return shopListOutDTO;
    }

    /**
     * 商户费率
     */
    @RequestMapping(value = "/agentRate")
    @ResponseBody
    public ShopListOutDTO AgentRate(ShopInDTO shopInDTO) {
        ShopListOutDTO agentRateList = new ShopListOutDTO();
        try {
            ShopListOutDTO agent = shopService.rate(shopInDTO.getGroupId(), shopInDTO.getType());
            agent.setErrorCode(Rescode.SUCCESS, "处理成功");
            return agent;
        } catch (ApplicationException ae) {
            logger.error(ae);
            agentRateList.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
        } catch (Exception e) {
            agentRateList.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return agentRateList;
    }

    /**
     * 用户费率
     */
    @RequestMapping(value = "/member/agentRate")
    @ResponseBody
    public ShopListOutDTO MemberAgentRate(ShopInDTO shopInDTO) {
        ShopListOutDTO agentRateList = new ShopListOutDTO();
        try {
            ShopListOutDTO agent = shopService.memberRate(shopInDTO.getGroupId(), shopInDTO.getType());
            agent.setErrorCode(Rescode.SUCCESS, "处理成功");
            return agent;
        } catch (ApplicationException ae) {
            logger.error(ae);
            agentRateList.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
        } catch (Exception e) {
            agentRateList.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return agentRateList;
    }


    /**
     * 创建门店
     */
    @RequestMapping(value = "/saveShop")
    @ResponseBody
    public ShopOutDTO SaveShop(@RequestBody ShopInDTO shopInDTO) {
        ShopOutDTO shopOutDTO = new ShopOutDTO();
        Boolean flag;
        try {
            flag = shopService.saveshop(shopInDTO);
            if (flag) {
                shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            } else {
                shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            }
            return shopOutDTO;
        } catch (ApplicationException ae) {
            shopOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        } catch (Exception e) {
            shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return shopOutDTO;
    }


    /**
     * 修改门店
     */
    @RequestMapping(value = "/editShop")
    @ResponseBody
    public ShopOutDTO editShop(@RequestBody ShopInDTO shopInDTO) {
        ShopOutDTO shopOutDTO = new ShopOutDTO();
        Boolean flag;
        try {
            flag = shopService.editShop(shopInDTO);
            if (flag) {
                shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

//					 String content ="修改门店信息";
//					 sysLogService.addSysLog(shopInDTO.getUserId(),shopInDTO.getUserName(),null,content,shopInDTO.getGroupId());
            } else {
                shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            }
            return shopOutDTO;
        } catch (ApplicationException e) {
            e.printStackTrace();
            shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, e.errMsg);
        } catch (Exception e) {
            e.printStackTrace();
            shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
        }
        return shopOutDTO;
    }


    /**
     * 修改门店账户
     */
    @RequestMapping(value = "/editShopAccount")
    @ResponseBody
    public ShopOutDTO editShopAccount(@RequestBody ShopInDTO shopInDTO) {
        ShopOutDTO shopOutDTO = new ShopOutDTO();
        Boolean flag;
        try {
            flag = shopService.editShopAccount(shopInDTO);
            if (flag) {
                shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

//					 String content ="修改门店信息";
//					 sysLogService.addSysLog(shopInDTO.getUserId(),shopInDTO.getUserName(),null,content,shopInDTO.getGroupId());
            } else {
                shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            }
            return shopOutDTO;
        } catch (ApplicationException e) {
            e.printStackTrace();
            shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, e.errMsg);
        } catch (Exception e) {
            e.printStackTrace();
            shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
        }
        return shopOutDTO;
    }

    /**
     * 添加门店余额
     */
    @RequestMapping(value = "/addBalance")
    @ResponseBody
    public BaseOutDTO addBalance(BalanceInDTO balanceInDTO) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        Boolean flag;
        try {
            flag = shopService.addBalance(balanceInDTO);
            if (flag) {
                baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            } else {
                baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            }
            return baseOutDTO;
        } catch (ApplicationException e) {
            e.printStackTrace();
            baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, e.errMsg);
        } catch (Exception e) {
            e.printStackTrace();
            baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
        }
        return baseOutDTO;
    }

    @RequestMapping(value = "/queryBalance")
    @ResponseBody
    public BaseOutDTO queryBalance(ShopInDTO shopInDTO) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        try {
            return shopService.selectShopInfo(shopInDTO);

        } catch (ApplicationException e) {
            e.printStackTrace();
            baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, e.errMsg);
        } catch (Exception e) {
            e.printStackTrace();
            baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
        }
        return baseOutDTO;
    }

    /**
     * 修改门店状态
     */
    @RequestMapping(value = "/updateShopState")
    @ResponseBody
    public BaseOutDTO updateShopState(ShopInDTO shopInDTO) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        try {
            BaseOutDTO base = shopService.updateShopState(shopInDTO);
            return base;
        } catch (ApplicationException e) {
            e.printStackTrace();
            baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, e.errMsg);
        } catch (Exception e) {
            e.printStackTrace();
            baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
        }
        return baseOutDTO;
    }


    /**
     * 保存二维码
     */
    @RequestMapping(value = "/saveShopCrcode")
    @ResponseBody
    public CrcodeOutDTO SaveShopCrcode(CrcodeInDTO shopCrcodeInDTO) {
        CrcodeOutDTO shopCrcodeOutDTO = new CrcodeOutDTO();
        try {
            Boolean flag = shopService.saveShopCrcode(shopCrcodeInDTO);
            if (flag) {
                shopCrcodeOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
                return shopCrcodeOutDTO;
            } else {
                shopCrcodeOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
                return shopCrcodeOutDTO;
            }
        } catch (Exception e) {
            shopCrcodeOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return shopCrcodeOutDTO;
    }

    /**
     * 根据大商户查询代理商
     */
    @RequestMapping(value = "/agentList")
    @ResponseBody
    public AgentListOutDTO AgentList(AgentInDTO agentInDTO) {
        AgentListOutDTO agentInfoOutDTO = null;
        try {
            agentInfoOutDTO = shopService.agentList(agentInDTO);
            agentInfoOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return agentInfoOutDTO;
        } catch (Exception e) {
            agentInfoOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return agentInfoOutDTO;
    }


    /**
     * 根据大商户查询代理商(正常和禁用)
     */
    @RequestMapping(value = "/agentList2")
    @ResponseBody
    public AgentListOutDTO AgentList2(AgentInDTO agentInDTO) {
        AgentListOutDTO agentInfoOutDTO = null;
        try {
            agentInfoOutDTO = shopService.agentList2(agentInDTO);
            agentInfoOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return agentInfoOutDTO;
        } catch (Exception e) {
            agentInfoOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return agentInfoOutDTO;
    }

    /**
     * 查询所有大商户
     */
    @RequestMapping(value = "/groupList")
    @ResponseBody
    public GroupListOutDTO GroupList(ShopInDTO shopInDTO) {
        GroupListOutDTO groupListOutDTO = null;
        try {
            groupListOutDTO = shopService.GroupList();
            groupListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return groupListOutDTO;
        } catch (Exception e) {
            groupListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return groupListOutDTO;
    }

    /**
     * 根据名称查询是否有此代理商
     */
    @RequestMapping(value = "/hasTagentName")
    @ResponseBody
    public AgentInfoOutDTO HasTagentName(ShopInDTO shopInDTO) {
        AgentInfoOutDTO agentInfoOutDTO = null;
        try {
            agentInfoOutDTO = shopService.selectAgentName(shopInDTO);
            agentInfoOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return agentInfoOutDTO;
        } catch (Exception e) {
            agentInfoOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return agentInfoOutDTO;
    }


    /**
     * 查询门店详情
     */
    @RequestMapping(value = "/selectShopInfo")
    @ResponseBody
    public ShopOutDTO AelectShopInfo(ShopInDTO shopInDTO) {
        ShopOutDTO shopOutDTO = null;
        try {
            shopOutDTO = shopService.selectShopInfo(shopInDTO);
            shopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return shopOutDTO;
        } catch (Exception e) {
            shopOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return shopOutDTO;
    }


    /**
     * 查询二维码详情
     */
    @RequestMapping(value = "/selectCrcodeInfo")
    @ResponseBody
    public CrcodeOutDTO SelectCrcodeInfo(ShopInDTO shopInDTO) {
        CrcodeOutDTO crcodeOutDTO = null;
        try {
            crcodeOutDTO = shopService.selectCrcode(shopInDTO.getId());
            crcodeOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return crcodeOutDTO;
        } catch (Exception e) {
            crcodeOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return crcodeOutDTO;
    }

    /**
     * 修改二维码
     */
    @RequestMapping(value = "/editCrcode")
    @ResponseBody
    public CrcodeOutDTO editShopCrcode(CrcodeInDTO shopCrcodeInDTO) {
        CrcodeOutDTO shopCrcodeOutDTO = new CrcodeOutDTO();
        try {
            Boolean flag = shopService.updateShopCrcode(shopCrcodeInDTO);
            if (flag) {
                shopCrcodeOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
                return shopCrcodeOutDTO;
            } else {
                shopCrcodeOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
                return shopCrcodeOutDTO;
            }
        } catch (Exception e) {
            shopCrcodeOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return shopCrcodeOutDTO;
    }

    @RequestMapping("/shopSelective")
    @ResponseBody
    public ShopSelectiveListOutDTO shopSelective(@RequestBody ShopSelectiveQueryDTO shopSelectiveQueryDTO) {
        return shopService.shopSelective(shopSelectiveQueryDTO);
    }

    @RequestMapping("/getNonemptyAgent")
    @ResponseBody
    public List<Map<String, String>> getNonemptyAgent(@RequestBody Long groupId) {
        return shopService.getNonemptyAgent(groupId);
    }

    @RequestMapping("/getNonemptyIndustry")
    @ResponseBody
    public List<Map<String, String>> getNonemptyIndustry(@RequestBody Long groupId) {
        return shopService.getNonemptyIndustry(groupId);
    }

    @RequestMapping("/getShopListById")
    @ResponseBody
    public List<Map> getShopListById(@RequestBody ShopDetailBean shopDetailBean) {
        return shopService.getShopListById(shopDetailBean);
    }

    @RequestMapping("/getShopListByIdAndActivityId")
    @ResponseBody
    public List<Map> getShopListByIdAndActivityId(@RequestBody Map<String, Object> param) {
        Long activityId = (long) (Integer) param.get("activityId");
        List shopIdList = (List) param.get("shopIds");
        return shopService.getShopListById2(shopIdList, activityId);
    }

    /*
    * 根据大商户ID查询所有门店
    * */
    @RequestMapping("/listGroupShop")
    @ResponseBody
    public ShopListOutDTO listGroupShop(@RequestBody Long groupId) throws ApplicationException {
        return shopService.listGroupShop(groupId);
    }


    /*
    * 检查当前商户是否车门店
    * */
    @RequestMapping("/checkYsShop")
    @ResponseBody
    public CheckOutDTO checkYsShop(ShopInDTO shopInDTO) {
        return shopService.checkYsShop(shopInDTO);
    }

    /*
    * 门店收银员列表
    */
    @RequestMapping("/listShopCashier")
    @ResponseBody
    public ShopCashierListOutDTO listShopCashier(ShopCashierInDTO shopCashierInDTO, Page page) {
        return shopService.listShopCashier(shopCashierInDTO, page);
    }

    @RequestMapping("/addShopCashier")
    @ResponseBody
    public BaseOutDTO addShopCashier(@RequestBody ShopCashierInDTO shopCashierInDTO) {
        return shopService.addShopCashier(shopCashierInDTO);
    }

    @RequestMapping("/changeStatus")
    @ResponseBody
    public BaseOutDTO changeStatus(@RequestBody ShopCashierInDTO shopCashierInDTO) {
        return shopService.changeStatus(shopCashierInDTO);
    }


    /*
    *  弹出框门店选择列表
    * */
    @RequestMapping("/shopSelect")
    @ResponseBody
    public ShopListOutDTO shopSelect(@RequestBody ShopSelectQueryDTO shopSelectQueryDTO) {
        return shopService.shopSelect(shopSelectQueryDTO);
    }



    /**
     * 查询账户门店余额
     * @param shopInDTO
     * @return
     */
    @RequestMapping(value="/queryShopAmount")
    @ResponseBody
    public MemberOutDTO queryAgentAmount(ShopInDTO shopInDTO){
        MemberOutDTO memberOutDTO=new MemberOutDTO();
        try {
            memberOutDTO =shopService.queryShopAmount(shopInDTO);
            memberOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return memberOutDTO;
        }catch (ApplicationException ae) {
            logger.error(ae);
            memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
        } catch (Exception e) {
            memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return memberOutDTO;
    }



    /**
     * 代理商钱包修改余额
     * @param balanceInDTO
     * @return
     */

    @RequestMapping(value="/addShopBalance")
    @ResponseBody
    public MemberOutDTO addShopBalance(BalanceInDTO balanceInDTO) {
        MemberOutDTO memberOutDTO = new MemberOutDTO();
        try {
            memberOutDTO =shopService.addShopBalance(balanceInDTO,ROOT_HOME_URL);
            return memberOutDTO;
        }catch (ApplicationException ae) {
            logger.error(ae);
            memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
        }catch (Exception e) {
            memberOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return memberOutDTO;

    }

}
